<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="90%">
        <el-form v-loading="showLoading" element-loading-text="上传中" element-loading-spinner="el-icon-loading" ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="状态">
            <!-- <el-input v-model="form.status"  /> -->
            <el-select v-model="form.status" placeholder="请选择" style="width: 100%;">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.type" placeholder="请选择" style="width: 100%;" @change="typeChange">
              <el-option v-for="item in optionsType" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标题">
            <el-input v-model="form.title"  />
          </el-form-item>
          <el-form-item label="简略介绍">
            <el-input v-model="form.abbreviation"  />
          </el-form-item>
          <el-form-item label="作者">
            <el-input v-model="form.author"  />
          </el-form-item>
          <el-form-item label="图片地址">
            <div style="display:flex;">
              <el-input v-model="form.rwImage" style="margin-right: 5px"/>
              <el-upload
                ref="upload"
                :limit="1"
                :headers="headers"
                :show-file-list="false"
                :before-upload="beforeUpload"
                :on-success="handleSuccess"
                :on-error="handleError"
                :action="baseApi + '/front/upload/uploadV1'"
              >
                <el-button type="primary">上传图片</el-button>
              </el-upload>
            </div>
            <el-image v-if="form.rwImage" :src="form.rwImage" style="width: 300px;height:200px;"></el-image>
          </el-form-item>
          <el-form-item label="详情">
            <el-input v-model="form.rwText"  />
            <Editor class="editor" :text="form.rwText" @changeText="changeText"/>
          </el-form-item>
          <!-- <el-form-item v-if="form.type === 2" label="外链">
            <el-input v-model="form.outUrl"  />
          </el-form-item> -->
          <!-- <el-form-item label="是否推荐位">
            <el-input v-model="form.recommend"  />
          </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2 || showLoading" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
             {{scope.row.status=='1' ? "正常" : scope.row.status=='0' ? '异常' : '错误'}}
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{scope.row.type=='1' ? "正常" : scope.row.type=='2' ? '外链' : '错误'}}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="author" label="作者" />
        <el-table-column prop="rwImage" label="图片地址">
          <template slot-scope="scope">
            <el-image class="vedioSuoluetu" v-if="scope.row.rwImage"  :src="scope.row.rwImage"></el-image>
            <span v-else></span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="outUrl" label="外链" /> -->
        <!-- <el-table-column prop="operator" label="作者" /> -->
        <!-- <el-table-column prop="recommend" label="是否推荐位" /> -->
        <el-table-column prop="createdAt" label="创建时间" />
        <el-table-column prop="updatedAt" label="修改时间" />
        <el-table-column v-if="checkPer(['admin','dccArticle:edit','dccArticle:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import Editor from "@/views/components/Editor";
import crudDccArticle from "@/api/dccArticle";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { mapGetters } from 'vuex'
import { getToken } from '@/utils/auth'

const defaultForm = {
  id: null,
  status: null,
  type: null,
  title: null,
  abbreviation: null,
  author: null,
  rwImage: null,
  rwText: null,
  outUrl: null,
  operator: null,
  recommend: null,
  createdAt: null,
  updatedAt: null
};
export default {
  name: "DccArticle",
  components: { pagination, crudOperation, rrOperation, udOperation, Editor },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "毒查查文章",
      url: "api/dccArticle",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudDccArticle }
    });
  },
  data() {
    return {
      headers: { 'Authorization': getToken() },
      options: [{
        value: 1,
        label: '正常'
      }, {
        value: 0,
        label: '异常'
      }],
      optionsType: [{
        value: 1,
        label: '正常'
      }, {
        value: 2,
        label: '外链'
      }],
      permission: {
        add: ["admin", "dccArticle:add"],
        edit: ["admin", "dccArticle:edit"],
        del: ["admin", "dccArticle:del"]
      },
      showLoading: false,
      rules: {}
    };
  },
  computed: {
    ...mapGetters(['baseApi'])
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    changeText(item) {
      this.form.rwText = item
    },
    typeChange(item) {
      if (item !== 2) {
        this.form.outUrl = ''
      }
    },
    beforeUpload (file) {
      let list = ['jpeg','png','jpeg','dds','psd','pdt','webp','xmp','gif','bmp','svg','tiff']
      let type = file.type.split('/')[1] ? file.type.split('/')[1] : ''
      if (list.indexOf(type) === -1) {
        this.$message.error('文件类型不正确')
        return false
      }
      this.showLoading = true
    },
    handleSuccess (response,file,fileList) {
      if (response && response.imgUrl) {
        this.form.rwImage = response.imgUrl
        this.$refs.upload.clearFiles()
        this.showLoading = false
        this.$message.success('上传成功')
      }
    },
    handleError () {
        this.showLoading = false
        this.$message.error('上传失败')
    }
  }
};
</script>

<style lang="scss" scoped>
.editor {
  >>> .warn-content {
    height: 0;
  }
  >>> .right {
    height: 502px;
    overflow-y: scroll;
  }
}
</style>
